Skip to content

Add a quantity field to the item create modal form. #615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

EdWorth120
Copy link
Contributor

@EdWorth120 EdWorth120 commented Apr 1, 2025

What type of PR is this?

  • feature

What this PR does / why we need it:

This PR adds a quantity field to the item create modal form to allow people set the quantity of the item being added without needing to left the form and access the edit item form to change the quantity.

Which issue(s) this PR fixes:

Fixes #614

Special notes for your reviewer:

The definition of the ItemCreate interface inside the file backend/internal/data/repo/repo_items.go was changed to add the quantity field.

This definition is used to create the following files:

(with task swag)
backend/app/api/static/docs/docs.go
backend/app/api/static/docs/swagger.json
backend/app/api/static/docs/swagger.yaml

(with task typescript-types)
frontend/lib/api/types/data-contracts.ts

I needed to use task -f to force the task to be run and the files cited above to be correctly generated.

Testing

Try to create an item, for me the text field validates correctly the entries if the user type a value that is not an integer, but in the language that my operational system is configured not the language Homebox is configured.

Try to create an item with the default quantity.
Try to create an item with another quantity.

I did not added any additional validation that the edit item page doesn't do.

Summary by CodeRabbit

  • New Features
    • Enabled users to specify the quantity when creating an item.
    • Updated the creation interface with a numeric input field that defaults to 1.
    • Enhanced the user experience with clearer labeling for item quantity.

Copy link
Contributor

coderabbitai bot commented Apr 1, 2025

Walkthrough

This PR introduces a new Quantity field for item creation across both the backend and frontend. The backend now accepts and stores a numeric quantity by extending the ItemCreate struct and updating the repository's creation method. Concurrently, the frontend create modal has been enhanced with an input field for the quantity, defaulting to 1, and a corresponding locale entry is now included for proper labeling.

Changes

File(s) Change Summary
backend/internal/data/repo/.../repo_items.go Added new Quantity int field to the ItemCreate struct and updated the Create method to include SetQuantity(data.Quantity) in the item creation query.
frontend/components/Item/CreateModal.vue
frontend/locales/en.json
Introduced a numeric input in the create modal (default value: 1) bound to form.quantity, updated the API call payload, and added a locale entry for "Item Quantity".

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant CM as CreateModal.vue
    participant API as API Server
    participant IR as ItemsRepository
    participant DB as Database

    U->>CM: Enter item details (including quantity)
    CM->>API: Send item creation request with quantity
    API->>IR: Call Create(data) with quantity
    IR->>DB: Execute insert query with SetQuantity(data.Quantity)
    DB-->>IR: Return created record
    IR-->>API: Return item details
    API-->>CM: Return success response
Loading

Assessment against linked issues

Objective Addressed Explanation
Add a quantity field to the item create modal form (#614)

Possibly related PRs

Security Recommendations

  • Input Validation: Ensure that the backend validates the Quantity field to accept only positive integers to prevent erroneous data entries.
  • Sanitization: Both frontend and backend should sanitize inputs associated with the quantity field to mitigate injection attacks.
  • Parameterization: Confirm that the database queries using the new field are parameterized to securely handle user input.
  • Error Handling: Implement robust error handling for any cases where the input may not conform to expected formats.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3870987 and bbca00a.

📒 Files selected for processing (1)
  • frontend/components/Item/CreateModal.vue (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • frontend/components/Item/CreateModal.vue
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: Backend Server Tests / Go
  • GitHub Check: build (linux/arm/v7)
  • GitHub Check: build (linux/arm64)
  • GitHub Check: build (linux/amd64)
  • GitHub Check: build (linux/arm/v7)
  • GitHub Check: build (linux/amd64)
  • GitHub Check: build (linux/arm64)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Inline review comments failed to post. This is likely due to GitHub's limits when posting large numbers of comments. If you are seeing this consistently it is likely a permissions issue. Please check "Moderation" -> "Code review limits" under your organization settings.

Actionable comments posted: 1

🔭 Outside diff range comments (1)
backend/internal/data/repo/repo_items.go (1)

63-579: 💡 Verification agent

❓ Verification inconclusive

Ensure the API is secured against negative quantities

While the UI uses a number input which might prevent negative values, the API should validate input to prevent setting negative quantities which would make no sense for inventory items.


🏁 Script executed:

#!/bin/bash
# Check if there's any validation for negative quantities in the codebase
grep -r "Quantity" --include="*.go" backend/internal/api/
grep -r "quantity.*valid" --include="*.go" backend/

Length of output: 166


Security Concern: Enforce Non-Negative Quantities in API

The current implementation of the data models (e.g., in backend/internal/data/repo/repo_items.go) shows that the Quantity field is defined as an int without any explicit validation against negative values. Although the UI may restrict input, this leaves the API vulnerable if a negative value is passed directly.

  • Issue: No validation is present on the backend to reject negative quantities.
  • Recommendation:
    • Add a validation rule (such as validate:"min=0") directly to the JSON binding struct fields (for example in ItemUpdate and other related input structs).
    • Alternatively, include a middleware or business logic check in the API endpoints that handle item creation or update to ensure the quantity is not negative.

This change will strengthen the API’s security posture by ensuring that negative quantities cannot be set, thereby preventing potential discrepancies in inventory data.

🧹 Nitpick comments (1)
frontend/components/Item/CreateModal.vue (1)

15-19: Consider adding input validation constraints

While the field uses type="number", it doesn't have min/max constraints or step validation. According to the PR description, non-integer inputs should be flagged.

 <FormTextField
   v-model="form.quantity"
   :label="$t('components.item.create_modal.item_quantity')"
   type="number"
+  min="1"
+  step="1"
 />
🛑 Comments failed to post (1)
backend/internal/data/repo/repo_items.go (1)

63-63: 💡 Verification agent

🧩 Analysis chain

Consider adding input validation for quantity

The Quantity field in the ItemCreate struct does not have any validation constraints. Consider adding validation to ensure that quantity is a positive integer.

Run this script to check how input validation is handled for other fields in the codebase:


🏁 Script executed:

#!/bin/bash
# Check how validation is handled for other fields in ItemCreate
grep -r "validate:\"" --include="*.go" backend/internal/data/repo/

Length of output: 1826


Action Required: Add Validation to Quantity Field

It appears that all other fields in the ItemCreate struct already enforce validation rules (for example, the Name and Description fields use tags like validate:"required,min=1,max=255"). However, the Quantity field is missing a similar validation tag to ensure it holds only positive values.

  • Proposed Change:
    Update the declaration of the Quantity field in backend/internal/data/repo/repo_items.go (around line 63) to:

        Quantity int `json:"quantity" validate:"min=1"`

    This change enforces that any provided quantity is at least 1 (i.e., a positive integer).

  • Security Recommendation:
    Always validate and sanitize user inputs to prevent any unexpected behavior or potential attack vectors. In this case, ensuring that Quantity is non-negative not only upholds business logic but also minimizes risks such as integer underflow/overflow issues.

@EdWorth120 EdWorth120 marked this pull request as draft April 3, 2025 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a quantity field to the item create modal form
1 participant